package cn.hpclub.server.controller.admin;

import java.io.File;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hpclub.server.model.SalesPromotion;
import cn.hpclub.server.service.ds.CmccRewardService;

public class CmccrewardController extends BaseAdminController<SalesPromotion>{

    private static Logger logger = LoggerFactory.getLogger(CmccrewardController.class);

    public void list(){
        CmccRewardService instance = CmccRewardService.getInstance();
        List<File> files = instance.getRewardFiles();
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        String msg = "";
        if(instance.isExporting()){
            msg = "数据导出中，请稍后再试!";
        } else if(files.isEmpty()){
            exportLastest5MonthData();
            msg = "数据导出中，请稍后再试!";
        } else{
            for(File file : files){
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("name", file.getName());
                map.put("size", getFileSize(file.length()));
                long lastModified = file.lastModified();
                map.put("lastModified", getLastModified(lastModified));
                map.put("digest", URLEncoder.encode(new String(Base64.encodeBase64(file.getName().getBytes()))));
                list.add(map);
                logger.debug("map:" + map.toString());
            }
        }
        setAttr("msg", msg);
        setAttr("fileList", list);
        render("/admin/cmccreward_list.tpl");
    }

    /**
     * @Title: exportLastest5MonthData
     * @Description:TODO void
     */
    private void exportLastest5MonthData(){
        CmccRewardService.getInstance().exportLastestMonthData(5);
    }

    /**
     * @Title: exportLastest5MonthData
     * @Description:TODO void
     */

    public void download(){
        String digest = getPara("digest");
        logger.debug("digest=" + digest);
        String fileName = new String(Base64.decodeBase64(digest));
        logger.debug("fileName=" + new String(fileName));
        String filePath = CmccRewardService.getInstance().getDirPath() + "/" + fileName;
        renderFile(new File(filePath));
    }

    /**
     * @Title: getLastModified
     * @Description:TODO
     * @param lastModified
     * @return String
     */

    private String getLastModified(long lastModified){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date(lastModified));
    }

    /**
     * @Title: getFileSize
     * @Description:TODO
     * @param size
     *            TODO
     * @return String
     */

    private String getFileSize(long size){
        if(size > 1024 * 1024){
            return String.format("%.2f MB", ((double)size) / (1024 * 1024));
        } else if(size > 1024){
            return String.format("%.2f KB", ((double)size) / 1024);
        } else{
            return String.format("%f B", (double)size);
        }
    }
}
